package com.haois.mp.database.module.remote.service.impl;

import com.google.gson.Gson;
import com.haois.mp.database.util.HttpClientUtil;
import com.haois.mp.database.commom.CommonConstants;
import com.haois.mp.database.module.remote.service.IGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/2.
 */
@Service("iGroupService")
public class GroupServiceImpl implements IGroupService {
    private static final Logger logger = LoggerFactory.getLogger(GroupServiceImpl.class);

    @Value("${defined.database.server-url}")
    private String serverUrl;

    private Gson gson = new Gson();

    @Override
    public Map getGroupList(String type, String access_token) {
        return this.getGroupList(type, access_token, new HashMap());
    }

    @Override
    public Map getGroupList(String type, String access_token, Map params) {
        params.put("type", type);
        String url = serverUrl + CommonConstants.USER_GROUP_LIST;
        String response = HttpClientUtil.sendGet(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织列表返回结果:{}", response);
        return result;
    }

    @Override
    public Map getGroupListSize(String type, String access_token) {
        String url = serverUrl + CommonConstants.USER_GROUP_LIST + "/size";
        String response = HttpClientUtil.sendGet(url, new HashMap(), access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织列表数返回结果:{}", response);
        return result;
    }

    @Override
    public Map getMemberByOid(String oid, String access_token) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.USER_GROUP_URL.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendGet(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织成员信息返回结果:{}", response);
        return result;
    }


    @Override
    public Map getDepTree(String oid, String access_token) {
        String url = serverUrl + CommonConstants.GROUP_DEPTREE_URL.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendGet(url, new HashMap(), access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织树结构返回结果:{}", response);
        return result;
    }


    @Override
    public Map getGroup(String oid, String access_token) {
        String url = serverUrl + CommonConstants.GROUP_URL.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendGet(url, new HashMap(), access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织详细信息返回结果:{}", response);
        return result;
    }

    @Override
    public Map getGroupList(String access_token) {
        String url = serverUrl + CommonConstants.USER_GROUP_LIST_ALL;
        String response = HttpClientUtil.sendGet(url, new HashMap(), access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织详细信息返回结果:{}", response);
        return result;
    }

    @Override
    public Map updateAuthorizeMemState(String oid, String access_token) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.GROUP_MEM_AUTHORIZE.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("更新对用户授权信息返回结果:{}", response);
        return result;
    }

    @Override
    public Map updateAuthorizeOrgState(String oid, String access_token) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.GROUP_ORG_AUTHORIZE.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("更新对组织授权信息返回结果:{}", response);
        return result;
    }

    @Override
    public Map joinGroup(String oid, String accessToken) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.GROUP_JOIN.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("加入组织返回结果:{}", response);
        return result;
    }

    @Override
    public Map joinGroupByMark(String mark, String accessToken) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("mark", mark);
        String url = serverUrl + CommonConstants.GROUP_JOIN.replace(CommonConstants.OID, "mark");
        String response = HttpClientUtil.sendPost(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("通过Mark关注组织返回结果:{}", response);
        return result;
    }

    @Override
    public Map getGroupRelation(String oid, String memberNum, String accessToken) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        params.put("memberNum", memberNum);
        String url = serverUrl + CommonConstants.GROUP_RELATION.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织和成员存在关系返回结果:{}", response);
        return result;
    }

    @Override
    public Map getGroupRelationByMark(String mark, String memberNum, String accessToken) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("mark", mark);
        params.put("memberNum", memberNum);
        String url = serverUrl + CommonConstants.GROUP_RELATION.replace(CommonConstants.OID, "mark");
        String response = HttpClientUtil.sendPost(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取组织和成员存在关系返回结果:{}", response);
        return result;
    }

    @Override
    public Map sendApply(String oid, String content, String excuse, String access_token) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        params.put("content", content);
        params.put("excuse", excuse);
        String url = serverUrl + CommonConstants.APPLY_ORG_JOIN.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("发送申请加入组织返回结果:{}", response);
        return result;
    }

    @Override
    public Map exitGroup(String oid, String access_token) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.GROUP_EXIT.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("退出所在组织返回结果:{}", response);
        return result;
    }

}
